<html>
<head>
  <title>python 语句（code）</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="Evernote Windows/307027 (zh-CN, DDL); Windows/6.1.0 (Win32);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="637"/>
<h1>python 语句（code）</h1>

<div>
<span><div><div><div><span style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(28, 51, 135); font-weight: bold;">print</span></span></div><div><span style="font-size: 13px; color: rgb(255, 0, 0); font-weight: bold; line-height: 1.6em;">print</span><span style="font-size: 13px; line-height: 1.6em;">语句可以向屏幕上输出指定的文字。比如输出'hello, world'，用代码实现如下：</span></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">&gt;&gt;&gt; print 'hello, world'</span></div></div><div><br/></div><div><span style="line-height: 1.45;">print语句也可以跟上多个字符串，用逗号“,”隔开，就可以连成一串输出：</span></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">&gt;&gt;&gt; print 'The quick brown fox', 'jumps over', 'the lazy dog'</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">The quick brown fox jumps over the lazy dog</span></div></div><div><br/></div><div><br/></div><div><br/></div><h2><span style="font-size: 18pt; color: rgb(28, 51, 135);">注释</span></h2><div>Python的注释以 # 开头，后面的文字直到行尾都算注释</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div style="font-size: 9pt;"><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># 这一行全部都是注释...</span></div><div style="font-size: 9pt;"><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print 'hello' # 这也是注释</span></div></div><div><span style="line-height: 20.7999992370605px;">注释还有一个巧妙的用途，就是一些代码我们不想运行，但又不想删除，就可以用注释暂时屏蔽掉：</span></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;"># 暂时不想运行下面一行代码:</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;"># print 'hello, python.'</span></div></div><div><br/></div><div><br/></div><div><br/></div><div><font style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(28, 51, 135); font-weight: bold;">if语句</span></font></div><div>如果 if 语句判断为 True，就会执行这个代码块。</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">age = 20</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">if age &gt;= 18:</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    print 'your age is', age</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    print 'adult'</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print 'END'</span></div></div><h2><span style="font-size: 16pt; color: rgb(28, 51, 135); font-weight: normal;">if-else</span></h2></div><div>        两种条件判断是“非此即彼”的，要么符合条件1，要么符合条件2，因此，完全可以用一个 <span style="color: rgb(255, 0, 0);">if ... else ...</span> 语句把它们统一起来：</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">if age &gt;= 18:</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    print 'adult'</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">else:</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    print 'teenager'</span></div></div><h2><span style="font-size: 18pt; color: rgb(28, 51, 135); font-weight: normal;">if-elif-else</span></h2><div>要避免嵌套结构的 if ... else ...，我们可以用 <span style="color: rgb(255, 0, 0);">if ... 多个elif ... else ...</span> 的结构，一次写完所有的规则：</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(255, 0, 0); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">if age &gt;= 18:</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(255, 0, 0); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    print 'adult'</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(255, 0, 0); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">elif age &gt;= 6:</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(255, 0, 0); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    print 'teenager'</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(255, 0, 0); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">elif age &gt;= 3:</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(255, 0, 0); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    print 'kid'</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(255, 0, 0); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">else:</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(255, 0, 0); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    print 'baby'</span></div></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><h2><span style="font-size: 18pt; color: rgb(28, 51, 135);">for循环</span></h2><div><span style="color: rgb(255, 0, 0);">list</span>或<span style="color: rgb(255, 0, 0);">tuple</span>可以表示一个有序集合。如果我们想依次访问一个list中的每一个元素呢？</div><div>如果list包含1万个元素，我们就不可能写1万行print。<span style="line-height: 1.45;">这时，循环就派上用场了。</span></div><div>Python的 <span style="color: rgb(255, 0, 0);">for</span> 循环就可以依次把list或tuple的每个元素迭代出来：</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">L = ['Adam', 'Lisa', 'Bart']</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(255, 0, 0); font-family: Monaco;">for</span> <span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">name</span> <span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(255, 0, 0); font-family: Monaco;">in</span> <span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">L:</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">        print name</span></div></div><div><br/></div><div><br/></div><h2><span style="font-size: 18pt; color: rgb(28, 51, 135);">while循环</span></h2><div>while 循环不会迭代 list 或 tuple 的元素，而是根据表达式判断循环是否结束。</div><div>比如要从 0 开始打印不大于 N 的整数：</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">N = 10</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">x = 0</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">while x &lt; N:</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    print x</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    x = x + 1</span></div></div><div style="margin-top: 1em; margin-bottom: 1em;">        while循环每次先<span style="color: rgb(255, 0, 0);">判断 x &lt; N</span>，如果为<span style="color: rgb(255, 0, 0);">True</span>，则执行循环体的代码块，否则，退出循环。<span style="line-height: 1.45;">在循环体内，</span><span style="color: rgb(255, 0, 0); line-height: 1.45;">x = x + 1</span> <span style="line-height: 1.45;">会让</span> <span style="color: rgb(255, 0, 0); line-height: 1.45;">x</span> <span style="line-height: 1.45;">不断增加，最终因为</span> <span style="color: rgb(255, 0, 0); line-height: 1.45;">x &lt; N</span> <span style="line-height: 1.45;">不成立而退出循环。</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="line-height: 1.45;">        如果没有这一个语句，</span><span style="font-weight: bold; line-height: 1.45;">while循环在判断 x &lt; N 时总是为True</span><span style="line-height: 1.45;">，就会无限循环下去，变成死循环，所以要特别留意while循环的退出条件。</span></div><div><br/></div><div><br/></div><div><br/></div><div><font style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(28, 51, 135); font-weight: bold;">break退出循环</span></font></div><div>用 for 循环或者 while 循环时，如果要在循环体内直接退出循环，可以使用 <span style="color: rgb(255, 0, 0);">break </span>语句。</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">sum = 0</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">x = 1</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">while True:</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    sum = sum + x</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    x = x + 1</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    if x &gt; 100:</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">        break</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">print sum</span></div></div><div>咋一看， while True 就是一个死循环，但是在循环体内，我们还判断了 <span style="color: rgb(255, 0, 0);">x &gt; 100</span> 条件成立时，用break语句退出循环，这样也可以实现循环的结束。</div><div><br/></div><div><br/></div><h2><br/></h2><h2><span style="font-size: 18pt; color: rgb(28, 51, 135);">continue继续循环</span></h2><div>        在循环过程中，可以用break退出当前循环，还可以用<span style="color: rgb(255, 0, 0);">continue</span>跳过后续循环代码，继续下一次循环。</div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><h2><span style="font-size: 18pt; color: rgb(28, 51, 135);">多重循环</span></h2><div>在循环内部，还可以嵌套循环，我们来看一个例子：</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(255, 0, 0); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">for x in ['A', 'B', 'C']:</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(255, 0, 0); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    for y in ['1', '2', '3']:</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(255, 0, 0); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">        print x + y</span></div></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="color: rgb(255, 0, 0);">x</span> 每循环一次，<span style="color: rgb(255, 0, 0);">y</span> 就会循环 3 次，这样，我们可以打印出一个全排列：</div><div>A1</div><div>A2</div><div>A3</div><div>B1</div><div>B2</div><div>B3</div><div>C1</div><div>C2</div><div>C3</div><div><br/></div><div><br/></div><div><br/></div><div><font style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(28, 51, 135); font-weight: bold;">遍历的时候使用函数</span></font></div><h3>任务</h3><div>由于上述set不能识别小写的名字，请改进set，使得 'adam' 和 'bart'都能返回True。</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">s = set(name.lower() for name in['Adam', 'Lisa', 'Bart', 'Paul'])</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print 'adam' in s</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print 'bart' in s</span></div></div><div><br/></div><div>求和：</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">L = xrange(1, 101)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print sum([i*i for i in L])</span></div></div><div><br/></div><div><br/></div><div><br/></div><div><font style="font-size: 18pt;"><span style="color: rgb(28, 51, 135); font-size: 18pt; font-weight: bold;">迭代</span></font></div><div><span style="-en-paragraph: true; font-weight: bold;"><span>    <span>    </span></span>因为 Python 的 for循环不仅可以用在list或tuple上，还可以作用在其他任何可迭代对象上。</span></div><div><span style="-en-paragraph: true;"><span>    <span>    </span></span>因此，迭代操作就是对于一个集合，无论该集合是有序还是无序，我们用 for 循环总是可以依次取出集合的每一个元素。</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div><font style="font-size: 12pt;"><span style="font-weight: bold; font-family: Monaco; color: rgb(51, 51, 51);">注意</span><span style="font-family: Monaco; color: rgb(51, 51, 51);">: 集合是指包含一组元素的数据结构，我们已经介绍的包括：</span></font></div><div><font style="font-size: 12pt;"><span style="font-family: Monaco; color: rgb(51, 51, 51);">1.</span> <span style="font-weight: bold; font-family: Monaco; color: rgb(51, 51, 51);">有序集合</span><span style="font-family: Monaco; color: rgb(51, 51, 51);">：list，tuple，str和unicode；</span></font></div><div><font style="font-size: 12pt;"><span style="font-family: Monaco; color: rgb(51, 51, 51);">2.</span> <span style="font-weight: bold; font-family: Monaco; color: rgb(51, 51, 51);">无序集合</span><span style="font-family: Monaco; color: rgb(51, 51, 51);">：set</span></font></div><div><font style="font-size: 12pt;"><span style="font-family: Monaco; color: rgb(51, 51, 51);">3.</span> <span style="font-weight: bold; font-family: Monaco; color: rgb(51, 51, 51);">无序集合并且具有 key-value 对</span><span style="font-family: Monaco; color: rgb(51, 51, 51);">：dict</span></font></div></div><div><br/></div><h2><font style="font-size: 18pt; font-weight: normal; color: rgb(28, 51, 135);">索引迭代</font></h2><div><span style="-en-paragraph: true;">Python中，</span><span style="-en-paragraph: true; font-weight: bold;">迭代永远是取出元素本身，而非元素的索引。</span></div><div style="-en-paragraph: true; margin-top: 1em; margin-bottom: 1em;"><span style="-en-paragraph: true;">对于有序集合，元素确实是有索引的。有的时候，我们确实想在 for 循环中拿到索引，怎么办？</span></div><div><span style="-en-paragraph: true;">方法是使用</span> <span style="-en-paragraph: true; color: #FF0000; font-weight: bold;">enumerate() 函数</span><span style="-en-paragraph: true;">：</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">&gt;&gt;&gt; L = ['Adam', 'Lisa', 'Bart', 'Paul']</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">&gt;&gt;&gt;</span> <span style="color: #0000FF; font-family: Monaco; font-size: 9pt;">for</span> <span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">index, name</span> <span style="color: #0000FF; font-family: Monaco; font-size: 9pt;">in</span> <span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">enumerate(L)</span><span style="color: #0000FF; font-family: Monaco; font-size: 9pt;">:</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">... print index, '-', name</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">...</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">0 - Adam</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">1 - Lisa</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">2 - Bart</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">3 - Paul</span></div></div><div><span style="-en-paragraph: true;">实际上，enumerate() 函数把：</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div><span style="font-weight: bold;">['Adam', 'Lisa', 'Bart', 'Paul']</span></div></div><div style="-en-paragraph: true; margin-top: 1em; margin-bottom: 1em;"><span style="-en-paragraph: true;">变成了类似：</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div><span style="font-weight: bold;">[(0, 'Adam'), (1, 'Lisa'), (2, 'Bart'), (3, 'Paul')]</span></div></div><div><span style="-en-paragraph: true;"><font style="font-size: 12pt; color: rgb(173, 0, 0);"><b><u>因此，迭代的每一个元素实际上是一个tuple</u></b></font></span></div><div><span style="-en-paragraph: true;">如果我们知道每个tuple元素都包含两个元素，for循环又可以进一步简写为：</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div><span style="color: rgb(0, 0, 255);">for index, name in enumerate(L):</span></div><div><span style="color: rgb(0, 0, 255);">    print index, '-', name</span></div></div><div><span>    <span>    </span></span>可见，索引迭代也不是真的按索引访问，而是由 enumerate() 函数自动把每个元素变成 (index, element) 这样的tuple，再迭代，就同时获得了索引和元素本身。</div><div><br/></div><h2><font style="font-size: 18pt; font-weight: normal; color: rgb(28, 51, 135);">迭代dict的value</font></h2><div><span style="-en-paragraph: true;">dict 对象有一个</span> <span style="-en-paragraph: true; font-weight: bold;">values() 方法</span><span style="-en-paragraph: true;">，这个方法把dict转换成一个包含所有value的list，这样，我们迭代的就是 dict的每一个 value：</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div>d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }</div><div>print d.values()</div><div># [85, 95, 59]</div><div>for v in d.values():    print v</div><div># 85</div><div># 95</div><div># 59</div></div><div><span style="-en-paragraph: true;">如果仔细阅读Python的文档，还可以发现，dict除了</span><span style="-en-paragraph: true; font-weight: bold;">values()</span><span style="-en-paragraph: true;">方法外，还有一个</span> <span style="-en-paragraph: true; font-weight: bold;">itervalues()</span> <span style="-en-paragraph: true;">方法，用</span> <span style="-en-paragraph: true; font-weight: bold;">itervalues()</span> <span style="-en-paragraph: true;">方法替代</span> <span style="-en-paragraph: true; font-weight: bold;">values()</span> <span style="-en-paragraph: true;">方法，迭代效果完全一样：</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div>d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }</div><div>print d.itervalues()</div><div># &lt;dictionary-valueiterator object at 0x106adbb50&gt;</div><div>for v in d.itervalues():</div><div>    print v</div><div># 85</div><div># 95</div><div># 59</div></div><div><br/></div><h2><font color="#1C3387" style="font-size: 18pt; font-weight: normal;">迭代dict的key和value</font></h2><div><ul><li>dict 对象的 <span style="font-weight: bold;">items()</span> 方法</li></ul><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">&gt;&gt;&gt; d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">&gt;&gt;&gt; print d.items()</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">[('Lisa', 85), ('Adam', 95), ('Bart', 59)]</span></div></div></div><div><br/></div><div><span style="-en-paragraph: true;">可以看到，items() 方法把dict对象转换成了包含tuple的list，我们对这个list进行迭代，可以同时获得key和value：</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div>&gt;&gt;&gt; for key, value in d.items():</div><div>...     print key, ':', value</div><div>...</div><div>Lisa : 85</div><div>Adam : 95</div><div>Bart : 59</div></div><div><ul><li><span style="line-height: 1.45;">和 values() 有一个 itervalues() 类似，</span> <span style="line-height: 1.45; font-weight: bold;">items()</span> <span style="line-height: 1.45;">也有一个对应的</span> <span style="line-height: 1.45; font-weight: bold;">iteritems()</span><span style="line-height: 1.45;">，iteritems() 不把dict转换成list，而是在迭代过程中不断给出 tuple，所以， iteritems() 不占用额外的内存。</span><br/></li></ul></div><div><br/></div><div><font style="font-size: 18pt; color: rgb(28, 51, 135);">列表生成式</font></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div>&gt;&gt;&gt; [x * x for x in range(1, 11)]</div><div>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]</div></div><div style="-en-paragraph: true; margin-top: 1em; margin-bottom: 1em;"><ul><li><span style="line-height: 1.45;">这种写法就是Python特有的列表生成式。利用列表生成式，可以以非常简洁的代码生成 list。</span><br/></li></ul></div><div><span style="-en-paragraph: true;">写列表生成式时，把要生成的元素 x * x 放到前面，后面跟 for 循环，就可以把list创建出来，</span></div><div><br/></div><div><ul><li><span style="line-height: 1.45;">使用</span><span style="line-height: 1.45; font-weight: bold;">for循环</span><span style="line-height: 1.45;">的迭代不仅可以迭代普通的list，还可以迭代dict。</span><br/></li></ul></div><div><br/></div><h2><font style="font-size: 18pt; font-weight: normal; color: rgb(173, 0, 0);">条件过滤</font></h2><div><span style="-en-paragraph: true;">列表生成式的 </span><span style="-en-paragraph: true; font-weight: bold;">for 循环后面还可以加上 if 判断</span><span style="-en-paragraph: true;">。例如：</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div>&gt;&gt;&gt; [x * x for x in range(1, 11)]</div><div>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]</div></div><div style="-en-paragraph: true; margin-top: 1em; margin-bottom: 1em;"><span style="-en-paragraph: true;">如果我们只想要偶数的平方，不改动 range()的情况下，可以加上 if 来筛选：</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div>&gt;&gt;&gt; [x * x for x in range(1, 11) if x % 2 == 0]</div><div>[4, 16, 36, 64, 100]</div></div><div><span style="-en-paragraph: true;">有了 if 条件，只有 if 判断为 True 的时候，才把循环的当前元素添加到列表中。</span></div><div><br/></div><h2><font color="#1C3387" style="font-size: 18pt; font-weight: normal;">多层表达式</font></h2><div><span>for循环可以嵌套，因此，在列表生成式中，也可以用多层</span> <span>for</span> <span>循环来生成列表。</span></div><div><span>对于字符串 'ABC' 和 '123'，可以使用两层循环，生成全排列：</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div>&gt;&gt;&gt; [m + n for m in 'ABC' for n in '123']</div><div>['A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3']</div></div><div><span>翻译成循环代码就像下面这样：</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div>L = []</div><div>for m in 'ABC':</div><div>    for n in '123':</div><div>        L.append(m + n)</div></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div></div><div><br/></div></span>
</div></body></html> 